import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'device_verify_platform_interface.dart';

/// An implementation of [DeviceVerifyPlatform] that uses method channels.
class MethodChannelDeviceVerify extends DeviceVerifyPlatform {

  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('flutter/device_verify');

  @override
  Future<String?> getDeviceToken({bool? refresh}) async {
    refresh = refresh ?? false;
    if(DeviceVerifyPlatform.deviceToken == "" || DateTime.now().millisecondsSinceEpoch > DeviceVerifyPlatform.invalidateTime
      || refresh){
      DeviceVerifyPlatform.deviceToken = await methodChannel.invokeMethod<String>('getDeviceToken');
      //设置5分钟过期重新获取
      DeviceVerifyPlatform.invalidateTime = DateTime.now().millisecondsSinceEpoch + 1000 * 60 * 5;
    }
    return DeviceVerifyPlatform.deviceToken;
  }

  @override
  Future<String?> refreshDeviceToken() async {
    return await methodChannel.invokeMethod<String>('getDeviceToken');
  }
}
